﻿//*********************************************************
//
// Copyright (c) Microsoft. All rights reserved.
//
//*********************************************************

#pragma once
#include "pch.h"
#include "D2DPanel.h"
#include "DirectXHelper.h"

#include <windows.ui.xaml.media.dxinterop.h>

using namespace Microsoft::WRL;
using namespace Platform;
using namespace Windows::ApplicationModel;
using namespace Windows::Foundation;
using namespace Windows::Foundation::Collections;
using namespace Windows::Graphics::Display;
using namespace Windows::System::Threading;
using namespace Windows::UI;
using namespace Windows::UI::Core;
using namespace Windows::UI::Input::Inking;
using namespace Windows::Storage::Streams;
using namespace Windows::UI::Xaml;
using namespace Windows::UI::Xaml::Input;
using namespace Windows::UI::Xaml::Media;
using namespace Windows::UI::Xaml::Interop;
using namespace Concurrency;
using namespace DirectX;
using namespace D2D1;
using namespace SharpDepend_UWP_DirectXInterop;
using namespace DX;

D2DPanel::D2DPanel()
{
    m_backgroundColor = ColorF(ColorF::Black);

    CreateDeviceIndependentResources();
    CreateDeviceResources();
    CreateSizeDependentResources();
}

void SharpDepend_UWP_DirectXInterop::D2DPanel::OnBeginDraw()
{
	if (!m_loadingComplete)
	{
		return;
	}

	m_d2dContext->BeginDraw();
	m_d2dContext->Clear(m_backgroundColor);
}

void SharpDepend_UWP_DirectXInterop::D2DPanel::OnDrawRectangle(
	float x1, float y1, float x2, float y2,
	float r, float g, float b, float a,
	int textureId,
	float td_x1, float td_y1, float td_x2, float td_y2,
	int textureWidth, int textureHeight
	)
{
	if (!m_loadingComplete)
	{
		return;
	}

}

void SharpDepend_UWP_DirectXInterop::D2DPanel::OnDrawLine(
	float x1, float y1, float x2, float y2,
	float r, float g, float b, float a
	)
{

}

void SharpDepend_UWP_DirectXInterop::D2DPanel::OnDrawCircle(
	float x, float y, float radius,
	float r, float g, float b, float a)
{

}

void SharpDepend_UWP_DirectXInterop::D2DPanel::OnDrawTriangles(Vector2Double* vertices, int verticesLength, unsigned short* indices, int indicesLength, ColorUInt* colors, int colorLength)
{

}

void SharpDepend_UWP_DirectXInterop::D2DPanel::OnEndDraw()
{
	if (!m_loadingComplete)
	{
		return;
	}

	m_d2dContext->EndDraw();

	Present();
}


void D2DPanel::OnRegisterTexture(int id, int* data, int dataLength, int width, int height)
{
	if (m_textures.count(id))
	{
		throw "ID texture already exist.";
	}

	ID2D1Bitmap* bitmap = nullptr;

	int pitch = sizeof(int) * width;

	auto hr = m_d2dContext->CreateBitmap(
		D2D1::SizeU(width, height),
		data,
		pitch,
		D2D1::BitmapProperties(
			// Format = RGB32
			D2D1::PixelFormat(DXGI_FORMAT_R8G8B8A8_UNORM, D2D1_ALPHA_MODE_PREMULTIPLIED)
			),
		&bitmap
		);

	m_textures[id] = bitmap;
}

void D2DPanel::OnUnregisterTexture(int id)
{
	ID2D1Bitmap* bitmap = m_textures[id];
	
	bitmap->Release();
	bitmap = nullptr;

	m_textures.erase(id);
}

void D2DPanel::Render()
{

}

void D2DPanel::CreateDeviceResources()
{
    DirectXPanelBase::CreateDeviceResources();

    m_d2dContext->CreateSolidColorBrush(ColorF(ColorF::Black), &m_strokeBrush);

    // Set D2D's unit mode to pixels so that drawing operation units are interpreted in pixels rather than DIPS. 
    m_d2dContext->SetUnitMode(D2D1_UNIT_MODE::D2D1_UNIT_MODE_PIXELS);

    m_loadingComplete = true;
}

void D2DPanel::OnDeviceLost()
{
    // Handle device lost, then re-render.
    DirectXPanelBase::OnDeviceLost();
    //Render();
}

void D2DPanel::OnSizeChanged(Platform::Object^ sender, SizeChangedEventArgs^ e)
{
    // Process SizeChanged event, then re-render at the new size.
    DirectXPanelBase::OnSizeChanged(sender, e);
    //Render();
}

void D2DPanel::OnCompositionScaleChanged(SwapChainPanel ^sender, Object ^args)
{
    // Process CompositionScaleChanged event, then re-render at the new scale.
    DirectXPanelBase::OnCompositionScaleChanged(sender, args);
    //Render();
}

void D2DPanel::OnResuming(Object^ sender, Object^ args)
{
    // Ensure content is rendered when the app is resumed.
    //Render();
}

